﻿Imports System.Web
Imports MySoft.Map.Data
Imports MySoft.Map.Application
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Xml
Imports MySoft.Slxt.Business
Imports System.IO
Imports MySoft.PubProject.Business
Imports System.Collections.Generic

Partial Public Class Zkgl_XMLHTTP
    Inherits System.Web.UI.Page
    Dim MyBizParamMNG As New MySoft.PubProject.Business.BizParamMNG
    Dim MySaleMNG As New MySoft.Slxt.Business.SaleMNG
    Dim MyRoomMNG As New MySoft.Slxt.Business.RoomMNG
    Dim MyBankMNG As New MySoft.Slxt.Business.BankMNG
    Dim MyOrder As New MySoft.Slxt.Business.Order
    Dim MyPubMng As New MySoft.PubProject.Business.PublicMNG
    Dim strSQL As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strYwType As String                 '业务类型
        Dim strOnlyFlag, strTxt As String       'resquest值
        Dim strReturn As String = ""            '返回值

        strYwType = Request.QueryString("ywtype")           '业务标识
        strOnlyFlag = Request.QueryString("ywonlyflag")     '唯一键值
        strTxt = Request.QueryString("ywtxt")               '其它参数值
        strYwType = Request.QueryString("ywtype")

        '接收post的长文本值
        Dim strLongText As String = ""                      'POST文本
        If Request.InputStream.Length > 0 Then
            Dim b(Request.InputStream.Length - 1) As Byte
            Request.InputStream.Read(b, 0, Request.InputStream.Length)
            strLongText = System.Text.Encoding.UTF8.GetString(b)
        End If

        Select Case strYwType.ToUpper
            Case "DeleteDiscountPlanAndDefine".ToUpper
                strReturn = DeleteDiscountPlanAndDefine(strLongText)           '删除：折扣方案和折扣项
                Exit Select
            Case "chkDiscntPlanRepeat".ToUpper
                strReturn = chkDiscntPlanRepeat(strLongText)           '效验：当前项目下折扣方案名称是否同名
                Exit Select
            Case "GetCalRule".ToUpper                         '计算折扣方案计算规则
                strReturn = GetCalRule(strTxt, strLongText)
                Exit Select
            Case "ImputDiscountPlan".ToUpper                                '功能：引入折扣方案
                strReturn = ImputDiscountPlan(strTxt, strLongText)
                Exit Select
            Case "GetRoomInfo".ToUpper                                '功能：引入折扣方案
                strReturn = GetRoomInfo(strLongText)
                Exit Select
            Case "TestPriceCalFktotal".ToUpper                                '功能：引入折扣方案
                strReturn = TestPriceCalFktotal(strTxt, strLongText)
                Exit Select
            Case "DiscountScopeRoomGUIDExist".ToUpper                                '效验：通过特定房间折扣GUID查询里面是否包含指定房间GUID
                strReturn = DiscountScopeRoomGUIDExist(strLongText)
                Exit Select
            Case "CheckIsEnabled".ToUpper                                '当前项目下是否存在启用（IsEnabled=1）的折扣方案
                strReturn = CheckIsEnabled()
                Exit Select
            Case "EnabledOperate".ToUpper                               '启用操作
                strReturn = EnabledOperate(strLongText)
            Case "GetRoomPayformName".ToUpper                               '获取当前房间所属项目下所有付款方式，包含项目付款方式和当前房间所属楼栋下的付款方式
                strReturn = GetRoomPayformName(strLongText)
            Case "GetCalStd".ToUpper                                    '获取'业务参数“成交总价计算方式”
                strReturn = GetCalStd(strLongText)
        End Select

        MyBase.Response.ContentType = "text/HTML"
        MyBase.Response.Clear()
        MyBase.Response.Write(strReturn)      ' 返回操作成功信息
        MyBase.Response.End()
    End Sub

#Region "业务处理代码"

    '获取当前房间所属项目下所有付款方式，包含项目付款方式和当前房间所属楼栋下的付款方式
    Private Function GetRoomPayformName(ByVal a_strPostValue As String) As String
        Dim strSQL As String = ""
        Try
            '传入的参数格式为：DiscntGUID|RoomGUID 
            Dim arrGUID() As String = a_strPostValue.Split("|")
            If arrGUID(0) <> String.Empty And arrGUID(1) <> String.Empty Then
                strSQL = String.Format("SELECT DisCount AS [value],PayformName AS [text] FROM s_PayForm WHERE (ProjGUID='{0}' AND Scope='项目' OR BldGUIDList LIKE '%{1}%') ORDER BY PayformName ", arrGUID(0), arrGUID(1))
            End If
            '定义临时table变量
            Dim dtTmp As DataTable
            dtTmp = MyDB.GetDataTable(strSQL)
            Dim oNewValuelist As List(Of String) = New List(Of String)
            Dim oNewTextlist As List(Of String) = New List(Of String)
            If dtTmp IsNot Nothing Then
                For index As Integer = 0 To dtTmp.Rows.Count - 1
                    oNewValuelist.Add(dtTmp.Rows(index)(0).ToString())
                    oNewTextlist.Add(dtTmp.Rows(index)(1).ToString())
                Next
                Return String.Join(";", oNewValuelist.ToArray()) & "|BldGUIDList|" + String.Join(";", oNewTextlist.ToArray())
            End If
            Return "NO"
        Catch ex As Exception
            Return "NO"
        End Try

    End Function


    '效验：通过特定房间折扣GUID查询里面是否包含指定房间GUID
    Private Function DiscountScopeRoomGUIDExist(ByVal a_strPostValue As String) As String
        Dim intCount As Integer
        Dim strSQL As String = ""
        '传入的参数格式为：DiscntGUID|RoomGUID 
        Dim arrGUID() As String = a_strPostValue.Split("|")
        If arrGUID(0) <> String.Empty And arrGUID(1) <> String.Empty Then
            strSQL = String.Format("SELECT COUNT(0) FROM s_DiscountScope WHERE DiscntGUID='{0}' AND RoomGUID='{1}'", arrGUID(0), arrGUID(1))
        End If
        Try
            intCount = MyDB.GetDataItemInt(strSQL)
            '如果大于0说明特定房间列表中包含指定的房间GUID
            If intCount > 0 Then
                Return "success"
            Else
                Return "fail"
            End If
        Catch ex As Exception
            Return "操作失败.请与管理员联系！"
        End Try
    End Function

    '根据房间GUID查询房间的相关信息
    Private Function GetRoomInfo(ByVal arrGUID As String) As String
        Dim strSQL As String = ""
        Try
            If arrGUID <> String.Empty Then
                strSQL = String.Format("SELECT distinct PR.RoomGUID,R.RoomInfo,R.ProductType,PR.isAnnexe,R.YsBldArea,R.YsTnArea,R.ScBldArea,R.ScTnArea,R.DjArea,R.Price,R.TnPrice,R.Total,'0.00' AS appForm_Null,PR.BldGUID FROM p_Room PR LEFT JOIN ep_Room R ON PR.RoomGUID=R.RoomGUID LEFT JOIN s_PayForm PF ON PF.ProjGUID=PR.ProjGUID WHERE (1=1) AND PR.RoomGUID='{0}' ORDER BY RoomInfo", arrGUID)
            End If
            '定义临时table变量
            Dim dtTmp As DataTable
            dtTmp = MyDB.GetDataTable(strSQL)
            If dtTmp IsNot Nothing Then
                '返回格式为：OK|RoomGUID|RoomInfo|ProductType|isAnnexe|YsBldArea|YsTnArea|ScBldArea|ScTnArea|DjArea|Price|TnPrice|Total|BldGUID
                Return String.Format("OK|{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}|{9}|{10}|{11}|{12}", dtTmp.Rows(0)(0).ToString(), dtTmp.Rows(0)(1).ToString(), dtTmp.Rows(0)(2).ToString(), dtTmp.Rows(0)(3).ToString(), dtTmp.Rows(0)(4).ToString(), dtTmp.Rows(0)(5).ToString(), dtTmp.Rows(0)(6).ToString(), dtTmp.Rows(0)(7).ToString(), dtTmp.Rows(0)(8).ToString(), dtTmp.Rows(0)(9).ToString(), dtTmp.Rows(0)(10).ToString(), dtTmp.Rows(0)(11).ToString(), dtTmp.Rows(0)(13).ToString())
            End If
            Return "NO"
        Catch ex As Exception
            Return "NO|操作失败，请关闭重试！"
        End Try

    End Function

    '引入折扣项目
    Private Function ImputDiscountPlan(ByVal a_strTxt As String, ByVal a_strFormValues As String) As String
        Try
            '保存传入参数分割后的值
            Dim arrParam() As String
            arrParam = a_strTxt.Split("|")
            Dim strSQL As String
            '当前项目GUID
            Dim strProjGUID As String = arrParam(1)
            '当前项目名称
            Dim strProjName As String = arrParam(2)
            '保存新生成的GUID，方便保存数据的主从关系
            Dim strNewGUID As String = ""
            '折扣方案名称，最终拼接SQL语句用
            Dim strDiscntPlanName As String = ""
            '存储计算折扣该名称后面的数字用
            Dim strSQLTemp As String

            '需要引入的折扣方案GUID数组
            '如果 当前项目名称和当前项目GUID都不为空
            If Not strProjGUID Is Nothing And Not strProjName Is Nothing And arrParam(0) <> String.Empty Then
                '计算编号用的临时变量
                Dim intTemp As String
                '循环判断新生成的编号是否在数据库中还有重复的用的
                Dim blnNext As Boolean
                '保存新的编号
                Dim strNo As String
                '保存折扣方案新生成的名称，临时变量
                Dim strZkfaNameTemp As String

                '赋值为选择的折扣方案名称
                strDiscntPlanName = arrParam(3)
                '记录是否需要在名称后面添加”-“符号
                Dim blnIsAddSplitText As Boolean = False

                '查看名称末位是否为数字并复制,如果不是数字编号复制为1
                If strDiscntPlanName.Length < 1 Then
                    intTemp = "1"
                Else
                    '判断需要引入的折扣方案名称中是否包含”-“符号
                    If strDiscntPlanName.LastIndexOf("-") > -1 Then
                        intTemp = strDiscntPlanName.Substring(strDiscntPlanName.LastIndexOf("-") + 1)
                        strDiscntPlanName = strDiscntPlanName.Substring(0, strDiscntPlanName.LastIndexOf("-") + 1)
                        blnIsAddSplitText = True
                    Else
                        intTemp = Convert.ToInt32(intTemp) + 1
                    End If


                    Try
                        Integer.Parse(intTemp)
                    Catch ex As Exception
                        intTemp = "1"
                        strDiscntPlanName = arrParam(3) & "-"
                        blnIsAddSplitText = True
                    End Try

                End If

                '决定是否给折扣方案名称结尾添加”-“符号
                strZkfaNameTemp = strDiscntPlanName & IIf(blnIsAddSplitText, "", "-")
                strDiscntPlanName = strDiscntPlanName & IIf(blnIsAddSplitText, "", "-")

                blnNext = True
                '循环判断新生成的编号是否在数据库中还有重复的。
                While (blnNext)
                    '获取新的编号
                    strNo = intTemp.ToString()

                    '如果用折扣方案名称超出大小范围则截取
                    Dim intNameLength As Integer
                    intNameLength = System.Text.Encoding.Default.GetBytes(strDiscntPlanName).Length
                    If (intNameLength + strNo.Length + 1) >= 100 Then
                        strZkfaNameTemp = strZkfaNameTemp.Substring(0, strZkfaNameTemp.Length - intTemp.ToString().Length - 2)

                        If System.Text.Encoding.Default.GetBytes(strZkfaNameTemp).Length >= 100 Then
                            strZkfaNameTemp = strZkfaNameTemp.Substring(0, 98)
                        End If
                        strZkfaNameTemp = strZkfaNameTemp & "-"
                        strZkfaNameTemp = strZkfaNameTemp & Integer.Parse(strNo)
                    Else
                        strZkfaNameTemp = strDiscntPlanName & Integer.Parse(strNo)
                    End If

                    '判断数据库中是否重名
                    If Integer.Parse(IsDiscntPlanName(strZkfaNameTemp, strProjGUID)) > 0 Then
                        intTemp = Convert.ToInt32(intTemp) + 1
                        '如果重名就还原为最初折扣方案名称
                        strZkfaNameTemp = strDiscntPlanName & IIf(blnIsAddSplitText, "", "-")
                        strDiscntPlanName = strDiscntPlanName & IIf(blnIsAddSplitText, "", "-")
                    Else
                        blnNext = False
                    End If
                End While
                strDiscntPlanName = strZkfaNameTemp

                '产生一个新的GUID
                strNewGUID = Guid.NewGuid().ToString
                '先拼接添加 折扣方案 Sql语句
                strSQL &= String.Format("INSERT INTO s_DiscountPlan(DiscntPlanGUID,ProjGUID,DiscntPlanName,ModifyOn,IsEnabled,ModifyBy,CalRule,BUGUID)" & _
                "SELECT '{0}','{1}','{2}',getdate(),0,'{3}',CalRule,'{4}' FROM s_DiscountPlan WHERE DiscntPlanGUID='{5}';", strNewGUID, strProjGUID, strDiscntPlanName, Session("userName"), Session("BUGUID"), arrParam(0))
                '再拼接添加 折扣定义 Sql语句
                strSQL &= String.Format("INSERT INTO s_Discountdefine(DiscntGUID,ProjGUID,DiscntPlanGUID,DiscntName,CalMethod,DiscntValue,PreferentialPrice,BgnDate,EndDate,Remark,DiscntScope,IsAllowChange,Sequence)" & _
                "SELECT newid(),'{0}','{1}',DiscntName,CalMethod,DiscntValue,PreferentialPrice,BgnDate,EndDate,Remark,DiscntScope,IsAllowChange,Sequence FROM s_Discountdefine WHERE DiscntPlanGUID='{2}';", strProjGUID, strNewGUID, arrParam(0))
            End If

            If MyDB.ExecSQL(strSQL) > -1 Then
                Return "success|" & strNewGUID
            Else
                Return "操作失败.请与管理员联系!"
            End If
        Catch ex As Exception
            Return "操作失败.请与管理员联系!"
        End Try
    End Function


    '判断折扣方案是否重名
    Function IsDiscntPlanName(ByVal a_strDiscntPlanName As String, ByVal a_strProjGUID As String) As String
        Dim strSql As String
        Try
            strSql = String.Format("SELECT DiscntPlanName FROM s_DiscountPlan WHERE DiscntPlanName='{0}'", a_strDiscntPlanName)
            If a_strProjGUID <> String.Empty Then
                strSql = strSql & " AND ProjGUID = '" & a_strProjGUID & "'"
            End If
            Return MyDB.GetRowsCount(strSql).ToString()
        Catch ex As Exception
            Return "操作失败.请与管理员联系!"
        End Try
    End Function


    '校验当前项目下折扣方案名称是否同名
    Private Function chkDiscntPlanRepeat(ByVal strDiscntPlanName As String) As String
        Dim strProjGUID As String = Request.QueryString("ProjGUID")
        Dim strDiscntPlanGUID As String = Request.QueryString("oid")

        Dim strSQL As String = "SELECT count(1)  FROM s_DiscountPlan WHERE DiscntPlanName='" & strDiscntPlanName & "'" & IIf(strDiscntPlanGUID = "", "", " AND DiscntPlanGUID <> '" & strDiscntPlanGUID & "'")
        Dim strWhere = "AND ProjGUID='" & strProjGUID & "'"
        strSQL += strWhere
        Dim intCount As Integer

        Try
            intCount = MyDB.GetDataItemInt(strSQL)
            If intCount > 0 Then
                Return "重复"
            End If
        Catch ex As Exception
            Return "NO|操作失败，请关闭重试！"
        End Try
    End Function

    '删除折扣方案和折扣项
    Private Function DeleteDiscountPlanAndDefine(ByVal arrGUID As String) As String
        Try
            Dim strSQL As String
            Dim strOperate As String = Request.QueryString("Operate")
            '如果 Operate=define 则只删除折扣项，否则同时删除折扣方案和折扣项
            If Not strOperate Is Nothing And strOperate = "define" Then
                '删除折扣项
                strSQL = String.Format("DELETE s_Discountdefine WHERE DiscntPlanGUID IN ('{0}')", arrGUID.Replace(",", "','"))
            Else
                '删除特殊折扣房间信息
                strSQL = String.Format("DELETE FROM s_DiscountScope WHERE DiscntGUID IN(SELECT DiscntGUID FROM s_DiscountDefine WHERE DiscntPlanGUID IN(SELECT DiscntPlanGUID FROM dbo.s_DiscountPlan WHERE DiscntPlanGUID='{0}' AND DiscntScope='特定房间'))", arrGUID.Replace(",", "','"))
                '删除折扣方案
                strSQL = strSQL & String.Format(";DELETE s_DiscountPlan WHERE DiscntPlanGUID IN ('{0}')", arrGUID.Replace(",", "','"))
                '删除折扣项
                strSQL = strSQL & String.Format(";DELETE s_Discountdefine WHERE DiscntPlanGUID IN ('{0}')", arrGUID.Replace(",", "','"))

            End If

            If MyDB.ExecSQL(strSQL) > -1 Then
                Return "success"
            Else
                Return "操作失败.请与管理员联系!"
            End If
        Catch ex As Exception
            Return "操作失败.请与管理员联系!"
        End Try
    End Function

    '功能:价格试算
    '参数1:a_strTxt         --项目GUID|单价|面积|总价
    '参数2:a_strDataXml     --折扣明细
    Public Function TestPriceCalFktotal(ByVal a_strTxt As String, ByVal a_strDataXml As String) As String
        Dim decArea As Decimal          '计算面积
        Dim decOldTotal As Decimal      '折后房价
        Dim decTotal As Decimal         '折后房价
        Dim decOldPrice As Decimal      '折前单价
        Dim decPrice As Decimal         '折后单价
        Dim decYhTotal As Decimal = 0   '优惠
        Dim decDiscount As Decimal      '折扣
        Dim strCalMethod As String      '计算类型
        Dim strDiscntName As String     '折扣名称
        Dim strDz As String             '折扣说明
        Dim strProjGUID As String       '项目GUID
        Dim strFkJsfs As String         '房款计算方式(1,以标准总价为基准;2,以标准单价为基准)
        Dim arrParam() As String
        Dim xmlDOM As New XmlDocument


        If a_strDataXml <> "" Then
            Dim strReturn As String = ""
            Dim i As Integer
            Dim xmlNodes As XmlNodeList

            Try
                xmlDOM.LoadXml(a_strDataXml)
            Catch ex As Exception
                Return "FAIL|折扣信息格式错误,请与管理员联系!"
                Exit Function
            End Try

            arrParam = a_strTxt.Split("|")

            '赋初值
            strProjGUID = arrParam(0)
            decPrice = arrParam(1)
            decArea = arrParam(2)
            decTotal = arrParam(3)
            decOldPrice = arrParam(1)
            decOldTotal = arrParam(3)
            strFkJsfs = arrParam(4)

            '房款计算方式(1,以标准总价为基准;2,以标准单价为基准)
            strFkJsfs = MyBizParamMNG.GetBizParamConfig("s_CalStd", strProjGUID)
            strFkJsfs = IIf(strFkJsfs = "", "1", strFkJsfs)

            '计算折扣
            Dim decDiscntValue As Decimal               '折扣明细对应的折扣值
            Dim decPreferentialPrice As Decimal = 0     '折扣明细对应的优惠

            Dim strPricesToRetain As String = MyBizParamMNG.GetBizParamConfig("s_PricesToRetain", strProjGUID) '成交价格保留位
            Dim iPricesToRetain As Integer
            If Not IsNumeric(strPricesToRetain) Then
                strPricesToRetain = "-1"
            End If
            iPricesToRetain = CInt(strPricesToRetain)

            Select Case strFkJsfs
                Case "2"
                    '以标准单价为基准                   
                    xmlNodes = xmlDOM.DocumentElement.SelectNodes("/dataxml/row")
                    If xmlNodes.Count > 0 Then
                        For i = 0 To xmlNodes.Count - 1
                            '获取折扣信息
                            strDiscntName = xmlNodes(i).SelectSingleNode("DiscntName").InnerText
                            strCalMethod = xmlNodes(i).SelectSingleNode("CalMethod").InnerText
                            If xmlNodes(i).SelectSingleNode("DiscntValue").InnerText = "" Then
                                decDiscntValue = 0
                            Else
                                decDiscntValue = Convert.ToDecimal(xmlNodes(i).SelectSingleNode("DiscntValue").InnerText)
                            End If
                            If xmlNodes(i).SelectSingleNode("PreferentialPrice").InnerText = "" Then
                                decPreferentialPrice = 0
                            Else
                                decPreferentialPrice = Convert.ToDecimal(xmlNodes(i).SelectSingleNode("PreferentialPrice").InnerText)
                            End If

                            Select Case strCalMethod
                                Case "打折"
                                    If iPricesToRetain = -1 Or iPricesToRetain > 15 Then '没有设置或者设置后清空或者设置大于15
                                        decPrice = decPrice * decDiscntValue / 100
                                    Else
                                        decPrice = MyPubMng.MyRound(decPrice * decDiscntValue / 100, iPricesToRetain)
                                    End If

                                    Exit Select

                                Case "减点"
                                    If iPricesToRetain = -1 Or iPricesToRetain > 15 Then '没有设置或者设置后清空或者设置大于15
                                        If decOldPrice > 0 Then
                                            '单价模式进行减点,则当前的折扣decDiscount=当前单价*100/标准单价 - 减点值
                                            decDiscount = decPrice * 100 / decOldPrice - decDiscntValue
                                        Else
                                            decDiscount = 1
                                        End If
                                        decPrice = decOldPrice * decDiscount / 100
                                    Else
                                        If decOldPrice > 0 Then
                                            '单价模式进行减点,则当前的折扣decDiscount=当前单价*100/标准单价 - 减点值
                                            decDiscount = MyPubMng.MyRound(MyPubMng.MyRound(decPrice * 100 / decOldPrice, iPricesToRetain) - decDiscntValue, iPricesToRetain)
                                        Else
                                            decDiscount = 1
                                        End If
                                        decPrice = MyPubMng.MyRound(decOldPrice * decDiscount / 100, iPricesToRetain)
                                    End If

                                    Exit Select

                                Case "单价优惠"
                                    '由于此种方式不存在"以总价为基准的计算方式"中，所以不需要进行判断
                                    If iPricesToRetain = -1 Or iPricesToRetain > 15 Then '没有设置或者设置后清空或者设置大于15
                                        decPrice = decPrice - decPreferentialPrice
                                    Else
                                        decPrice = MyPubMng.MyRound(decPrice - decPreferentialPrice, iPricesToRetain)
                                    End If

                                    Exit Select

                                Case "总价优惠"
                                    If iPricesToRetain = -1 Or iPricesToRetain > 15 Then '没有设置或者设置后清空或者设置大于15
                                        decYhTotal = decYhTotal + decPreferentialPrice

                                        decTotal = decPrice * decArea

                                        decTotal = decTotal - decPreferentialPrice
                                    Else
                                        decYhTotal = MyPubMng.MyRound(decYhTotal + decPreferentialPrice, iPricesToRetain)

                                        decTotal = MyPubMng.MyRound(decPrice * decArea, iPricesToRetain)

                                        decTotal = MyPubMng.MyRound(decTotal - decPreferentialPrice, iPricesToRetain)
                                    End If


                                    If decArea > 0 Then
                                        If iPricesToRetain = -1 Or iPricesToRetain > 15 Then '没有设置或者设置后清空或者设置大于15
                                            decPrice = decTotal / decArea
                                        Else
                                            decPrice = MyPubMng.MyRound(decTotal / decArea, iPricesToRetain)
                                        End If
                                    Else
                                        decPrice = -1
                                    End If

                                    Exit Select
                            End Select
                        Next
                    End If

                    If iPricesToRetain = -1 Or iPricesToRetain > 15 Then '没有设置或者设置后清空或者设置大于15                        
                        decTotal = decPrice * decArea
                    Else
                        decTotal = MyPubMng.MyRound(decPrice * decArea, iPricesToRetain)
                    End If

                    Exit Select

                Case "1"

                    '以标准总价为基准

                    xmlNodes = xmlDOM.DocumentElement.SelectNodes("/dataxml/row")
                    If xmlNodes.Count > 0 Then
                        For i = 0 To xmlNodes.Count - 1
                            '获取折扣信息
                            strDiscntName = xmlNodes(i).SelectSingleNode("DiscntName").InnerText
                            strCalMethod = xmlNodes(i).SelectSingleNode("CalMethod").InnerText
                            If xmlNodes(i).SelectSingleNode("DiscntValue").InnerText = "" Then
                                decDiscntValue = 0
                            Else
                                decDiscntValue = Convert.ToDecimal(xmlNodes(i).SelectSingleNode("DiscntValue").InnerText)
                            End If
                            If xmlNodes(i).SelectSingleNode("PreferentialPrice").InnerText = "" Then
                                decPreferentialPrice = 0
                            Else
                                decPreferentialPrice = Convert.ToDecimal(xmlNodes(i).SelectSingleNode("PreferentialPrice").InnerText)
                            End If
                            Select Case strCalMethod
                                Case "打折"
                                    If iPricesToRetain = -1 Or iPricesToRetain > 15 Then  '没有设置或者设置后清空或者设置大于15
                                        decTotal = decTotal * decDiscntValue / 100
                                    Else
                                        decTotal = MyPubMng.MyRound(decTotal * decDiscntValue / 100, iPricesToRetain)
                                    End If

                                    Exit Select

                                Case "减点"
                                    If iPricesToRetain = -1 Or iPricesToRetain > 15 Then '没有设置或者设置后清空或者设置大于15
                                        If decOldTotal > 0 Then
                                            '总价模式进行减点,则当前的折扣decDiscount=当前总价*100/标准总价 - 减点值 
                                            decDiscount = decTotal * 100 / decOldTotal - decDiscntValue
                                        Else
                                            decDiscount = 1
                                        End If
                                        decTotal = decOldTotal * decDiscount / 100
                                    Else
                                        If decOldTotal > 0 Then
                                            '总价模式进行减点,则当前的折扣decDiscount=当前总价*100/标准总价 - 减点值 
                                            decDiscount = MyPubMng.MyRound(MyPubMng.MyRound(decTotal * 100 / decOldTotal, iPricesToRetain) - decDiscntValue, iPricesToRetain)
                                        Else
                                            decDiscount = 1
                                        End If
                                        decTotal = MyPubMng.MyRound(decOldTotal * decDiscount / 100, iPricesToRetain)
                                    End If

                                    Exit Select

                                Case "总价优惠"

                                    If iPricesToRetain = -1 Or iPricesToRetain > 15 Then '没有设置或者设置后清空或者设置大于15
                                        decTotal = decTotal - decPreferentialPrice
                                    Else
                                        decTotal = MyPubMng.MyRound(decTotal - decPreferentialPrice, iPricesToRetain)
                                    End If

                                    Exit Select
                            End Select
                        Next
                    End If

                    Exit Select
            End Select

            Dim decFnlTotal, decFnlDiscnt, decFnlPrice As Decimal
            Dim intPriceZero As Integer = MySaleMNG.GetSysZero("s_PriceZero", strProjGUID)              '单价保留位
            Dim intTotalZero As Integer = MySaleMNG.GetSysZero("s_TotalZero", strProjGUID)               '总价保留位

            If decOldTotal > 0 Then
                '折扣总价
                decFnlTotal = MyPubMng.MyRound(decTotal, intTotalZero)
                '优惠比例 (折扣总价/折后房价)*100
                decFnlDiscnt = MyPubMng.MyRound(decFnlTotal / decOldTotal, 4) * 100
                '折扣单价 (折扣总价/面积) 精确到元
                decFnlPrice = MyPubMng.MyRound(decFnlTotal / decArea, 0)
            End If

            Return "OK|" & decFnlDiscnt.ToString & "|" & strDz & "|" & decFnlPrice.ToString & "|" & decFnlTotal.ToString
        Else
            Return "FAIL|无法获取房间信息或者折扣信息,请与管理员联系!"
        End If

    End Function

    '计算折扣方案计算规则
    Public Function GetCalRule(ByVal strTxt As String, ByVal strDataXml As String) As String
        Dim xmlDOM As New XmlDocument
        If strDataXml <> "" Then
            Dim strReturn As String = ""
            Dim i As Integer
            Dim xmlNodes As XmlNodeList

            Try
                xmlDOM.LoadXml(strDataXml)
            Catch ex As Exception
                Return "FAIL|折扣信息格式错误,请与管理员联系!"
                Exit Function
            End Try

            Dim strProjGUID As String = strTxt

            '获取成交总价计算方式 (1,以标准总价为基准;2,以标准单价为基准)
            Dim strCalStd As String = MyBizParamMNG.GetBizParamConfig("s_CalStd", strProjGUID)
            strCalStd = IIf(strCalStd = "", "1", strCalStd)

            Dim strDiscntName, strCalMethod, strDiscntValue, strPreferentialPrice As String

            Dim strCalRule As String = ""

            If strCalStd = "1" Then
                strCalRule = "标准总价*付款方式折扣值"
            Else
                strCalRule = "标准单价*成交面积*付款方式折扣值/成交面积"
            End If

            xmlNodes = xmlDOM.DocumentElement.SelectNodes("/dataxml/row")
            For i = 1 To xmlNodes.Count - 1
                '获取折扣信息
                strDiscntName = xmlNodes(i).SelectSingleNode("DiscntName").InnerText
                strCalMethod = xmlNodes(i).SelectSingleNode("CalMethod").InnerText
                strDiscntValue = xmlNodes(i).SelectSingleNode("DiscntValue").InnerText
                strPreferentialPrice = xmlNodes(i).SelectSingleNode("PreferentialPrice").InnerText

                Select Case strCalMethod
                    Case "打折"
                        If strCalStd = "1" Then
                            strCalRule = strCalRule + "*打折" + strDiscntValue + "%"
                        Else
                            strCalRule = strCalRule + "*成交面积*打折" + strDiscntValue + "%/成交面积"
                        End If

                    Case "减点"
                        If strCalStd = "1" Then
                            strCalRule = "(" + strCalRule + "/标准总价-减" + strDiscntValue + "点)*标准总价"
                        Else
                            strCalRule = "(" + strCalRule + "*成交面积/标准总价-减" + strDiscntValue + "点)*标准总价/成交面积"
                        End If

                    Case "单价优惠"
                        '成交总价计算方式”的值为“以标准总价为基准”的时候，折扣类型为“单价优惠”的折扣项不参与折扣方案计算规则的计算
                        If strCalStd <> "1" Then
                            strCalRule = "(" + strCalRule + "-单价优惠" + strPreferentialPrice + "元" + ")"
                        End If

                    Case "总价优惠"
                        If strCalStd = "1" Then
                            strCalRule = "(" + strCalRule + "-总价优惠" + strPreferentialPrice + "元" + ")"
                        Else
                            strCalRule = "(" + strCalRule + "*成交面积-总价优惠" + strPreferentialPrice + "元)/成交面积"
                        End If
                End Select
            Next
            '去除多余括号
            If strCalRule.IndexOf("(") = 0 And strCalRule.LastIndexOf(")") = strCalRule.Length - 1 Then
                strCalRule = strCalRule.Substring(1, strCalRule.Length - 2)
            End If


            Return strCalRule
        Else
            Return "FAIL|无法获取房间信息或者折扣信息,请与管理员联系!"
        End If

    End Function

    '当前项目下是否存在启用（IsEnabled=1）的折扣方案
    Public Function CheckIsEnabled() As String
        Dim strProjGUID As String = MysRequest.QueryString("ProjGUID")      '项目GUID
        Dim strOid As String = MysRequest.QueryString("oid")      '折扣方案GUID
        Dim strSQL As String
        strSQL = String.Format("SELECT count(1)  FROM s_DiscountPlan WHERE ProjGUID='{0}' AND IsEnabled=1 AND CONVERT(VARCHAR(100),DiscntPlanGUID)<>'{1}'", strProjGUID, strOid)
        Dim intCount As Integer
        Try
            intCount = MyDB.GetDataItemInt(strSQL)
            If intCount > 0 Then
                Return "Yes"
            End If
        Catch ex As Exception
            Return "NO|操作失败，请关闭重试！"
        End Try

    End Function

    '启用操作
    Public Function EnabledOperate(ByVal strGUID As String) As String
        Try
            Dim arrGUID() As String = strGUID.Split("|")
            Dim strDiscntPlanName As String = arrGUID(1)
            Dim strProjGUID As String = arrGUID(0)      '项目GUID
            Dim strSQL As String = ""

            strSQL = String.Format("UPDATE s_DiscountPlan SET IsEnabled=0 WHERE DiscntPlanGUID IN (SELECT DiscntPlanGUID FROM s_DiscountPlan WHERE IsEnabled=1 AND ProjGUID='{0}' AND DiscntPlanGUID<>'{1}')", strProjGUID, strDiscntPlanName)
            If MyDB.ExecSQL(strSQL) >= 0 Then
                Return "success"
            Else
                Return "操作失败.请与管理员联系!"
            End If
        Catch ex As Exception
            Return "操作失败.请与管理员联系!"
        End Try

    End Function
    '获取'业务参数“成交总价计算方式”
    Public Function GetCalStd(ByVal strProjGUID As String) As String
        '业务参数“成交总价计算方式”(1,以标准总价为基准;2,以标准单价为基准)
        Dim MyBizParamMNG As New Mysoft.PubProject.Business.BizParamMNG
        Dim strReturn As String = MyBizParamMNG.GetBizParamConfig("s_CalStd", strProjGUID)
        Return strReturn
    End Function
#End Region

End Class